#include "myQueue.h"
#include <malloc.h>

/** Initialize your data structure here. */
MyQueue* myQueueCreate(int maxSize) {
    MyQueue *mq = malloc(sizeof(MyQueue));
    mq->size = maxSize;
    mq->queue = malloc(sizeof(TDQTYPE)*maxSize);
    mq->tail = mq->head = 0;
    mq->count = 0;
    return mq;
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, TDQTYPE x) {
    obj->queue[obj->head++] = x;
    obj->count++;
    if(obj->head == obj->size) obj->head = 0;
}

/** Removes the element from in front of queue and returns that element. */
TDQTYPE myQueuePop(MyQueue* obj) {
    TDQTYPE x = obj->queue[obj->tail++];
    if(obj->tail == obj->size) obj->tail = 0;
    obj->count--;
    return x;
}

/** Get the front element. */
TDQTYPE myQueuePeek(MyQueue* obj) {
    return obj->queue[obj->tail];
}

/** Returns whether the queue is empty. */
int myQueueEmpty(MyQueue* obj) {
    return obj->count == 0;
}

void myQueueFree(MyQueue* obj) {
    free(obj->queue);
    free(obj);
}

//************TestCode************
#include <stdio.h>

int main(void)
{
    MyQueue *mq = myQueueCreate(10);
    printf("EmptyQueue:%d\n", myQueueEmpty(mq));
    int i;
    for(i=0; i<10; i++) {
       myQueuePush(mq, i+65);
    }
    printf("first queue ele:%c\n", myQueuePeek(mq));
    for(i=0; i<10; i++) {
        printf("%c\n", myQueuePop(mq));
    }
    myQueueFree(mq);
    return 0;

}


/********************************/
